package com.tang;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 延时消息生产者
 */
public class DelayProducer {
    public static void main(String[] args) throws Exception {
        //1.创建生成者对象
        DefaultMQProducer producer = new DefaultMQProducer("producer_group",true);

        //2.为生产者对象设置nameServer地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //3.把我们的生产者直接启动起来
        producer.start();
        //4.创建消息，发送消息
        for (int i = 0; i < 3; i++) {
            Date date=new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formatDate = simpleDateFormat.format(date);
            System.out.println(formatDate);
            String info="message from delay-producer :"+i+" "+formatDate;
            Message msg = new Message("custom-delay-topic", "delayTag", "custom-delay-key",info.getBytes());
            //"1s 5s 10s 30s 1m 2m 3m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h"  总共十八个等级
            msg.setDelayTimeLevel(2);
            producer.send(msg);
        }
        System.out.println("发送成功");
    }
}